package com.example.wifiscan;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;

public class WiFiDemoActivity extends Activity
 {      
	
	private final Handler handler = new Handler();
	
	ImageView ap1,ap2,ap3,ap4,objeto;
	
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        
        ap1 = (ImageView) findViewById(R.id.ImageView01);
        ap2 = (ImageView) findViewById(R.id.imageView1);
        ap3 = (ImageView) findViewById(R.id.ImageView02);
        ap4 = (ImageView) findViewById(R.id.ImageView03);
        objeto = (ImageView) findViewById(R.id.imageView2);
        
        doInback();
        
    }
    
    /**
     * 
     * Referência:
     * 
     * Indoor Location Tracking using Received Signal Strength Indicator
     * 
     * Cáculo trilateração:
     * 
     * d1^2 = (x1-x)^2 + (y1-y)^2
     * d2^2 = (x2-x)^2 + (y2-y)^2
     * d3^2 = (x3-x)^2 + (y3-y)^2
     * 
     * Rearranjando a equação temos:
     * 
     * x = AY32 + BY13 + CY21/2(x1Y32 + x2Y13 + x3Y21)
     * y = AX32 + BX13 + CX21/2(y1X32 + y2X13 + y3X21)
     * 
     * Onde 
     * A = x1^2 + y1^2 - d1^2
     * B =  x2^2 + y2^2 - d2^2
     * C = x3^2 + y3^2 - d3^2
     * 
     * e 
     * 
     * X32 = x3 - x2
     * X13 = x1 - x3
     * X21 = x2 - x1
     * 
     * Y32 = y3 - y2
     * Y13 = y1 - y3
     * Y21 = y2 - y1
     * 
     * 
     * Outra equação para verifiação:
     * 
     * x = (u^2+(d1^2 - d2^2))/2u;
     * y = (v^2+(d1^2 - d3^2))/2v;
     * 
     * u = x3-x1
     * v = x2-x1
     */
    public void doInback()
    {
        handler.postDelayed(new Runnable() {

            @Override
            public void run()
            {
            	int xy[] = new int[2];
            	ap3.getLocationOnScreen(xy);
            	int x1 = xy[0];
            	int y1 = xy[1];
            	
            	ap4.getLocationOnScreen(xy);
            	int x2 = xy[0];
            	int y2 = xy[1];
            	
            	ap1.getLocationOnScreen(xy);
            	int x3 = xy[0];
            	int y3 = xy[1];
            	
            	
            	for(int i= 5; i < 200; i+=5){
            		objeto.getLocationOnScreen(xy);
                	int xo = xy[0];
                	int yo = xy[1];
                	
            		int d1 = 100 - i;
            		int d2 = 100 + (i/2);
            		int d3 = 100 + i;
            		
            		int A = x1^2 + y1^2 - d1^2;
            		int	B =  x2^2 + y2^2 - d2^2;
            		int	C = x3^2 + y3^2 - d3^2;
            		
            		int X32 = x3 - x2;
            		int X13 = x1 - x3;
            		int X21 = x2 - x1;
            		int Y32 = y3 - y2;
            		int Y13 = y1 - y3;
            		int Y21 = y2 - y1;
            		
            		double x = (A*Y32 + B*Y13 + C*Y21)/2*(x1*Y32 + x2*Y13 + x3*Y21);
            		double y = (A*X32 + B*X13 + C*X21)/2*(y1*X32 + y2*X13 + y3*X21);
            		
                	
//            		AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(10,10, xo+new Double(x).intValue(), yo+new Double(y).intValue());
//            		
//            		objeto.setLayoutParams(param);
            		
            		int u = x3-x1;
                    int v = x2-x1;
                    	     
            		x = (u^2+(d1^2 - d2^2))/2*u;
            	    y = (v^2+(d1^2 - d3^2))/2*v;
            	    
            	    AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(10,10, xo+new Double(x).intValue(), yo+new Double(y).intValue());
            		
            		objeto.setLayoutParams(param);
            	     
            			     
            	}
            	
            	
                doInback();
            }
        }, 1000);

    }
    

}
